﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 
using System.Data.Common;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TSystemMenuDAL
    {
        public int Add_SystemMenu(TSystemMenuEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TSystemMenu(");
            strSql.Append("HeadID,IsShow,Family,Menu_Name,Url1,Image,Mark)");
            strSql.Append(" values (");
            strSql.Append("@HeadID,@IsShow,@Family,@Menu_Name,@Url1,@Image,@Mark)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HeadID", DbType.Int32, entity.HeadID);
            db.AddInParameter(dbCommand, "IsShow", DbType.String, entity.IsShow);
            db.AddInParameter(dbCommand, "Family", DbType.String, entity.Family);
            db.AddInParameter(dbCommand, "Menu_Name", DbType.String, entity.Menu_Name);
            db.AddInParameter(dbCommand, "Url1", DbType.String, entity.Url1);
            db.AddInParameter(dbCommand, "Image", DbType.String, entity.Image);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// 系统模块功能块使用权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void Update_SystemMenuShow(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE  TSystemMenu SET IsShow='1' WHERE ID=@ID ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.String, id);
                db.ExecuteNonQuery(dbCommand);
            }
        }
        /// <summary>
        /// 系统模块功能块使用权限
        /// </summary>
        public void Update_SystemMenuDefault()
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE  TSystemMenu SET IsShow='2' ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                db.ExecuteNonQuery(dbCommand);
            }
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        public bool Update_SystemMenu(TSystemMenuEntity entity)
        {
            bool falg = false;
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE  TSystemMenu SET IsShow=@IsShow,Family=@Family,Menu_Name=@Menu_Name,Url1=@Url1,Mark=@Mark  WHERE ID=@ID ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.String, entity.ID);
                db.AddInParameter(dbCommand, "IsShow", DbType.String, entity.IsShow);
                db.AddInParameter(dbCommand, "Family", DbType.String, entity.Family);
                db.AddInParameter(dbCommand, "Menu_Name", DbType.String, entity.Menu_Name);
                db.AddInParameter(dbCommand, "Url1", DbType.String, entity.Url1);
                db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
                if (db.ExecuteNonQuery(dbCommand) > 0)
                {
                    falg = true;
                }
                return falg;
            }
        }

        /// <summary>
        /// 排序
        /// </summary>
        public bool Update_SystemMenuFamily(int id, string family)
        {
            bool falg = false;
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE  TSystemMenu SET Family=@Family WHERE ID=@ID ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.String, id);
                db.AddInParameter(dbCommand, "Family", DbType.String, family);
                if (db.ExecuteNonQuery(dbCommand) > 0)
                {
                    falg = true;
                }
                return falg;
            }
        }
        /// <summary>
        /// GetSystemMenuByFamily
        /// </summary>
        public TSystemMenuEntity GetEntity_SystemMenuByFamily(string family)
        {
            string strSQL = "SELECT * FROM TSystemMenu WHERE Family=@Family";
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL))
            {
                db.AddInParameter(dbCommand, "Family", DbType.String, family);
                TSystemMenuEntity objInfo = new TSystemMenuEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        objInfo.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        objInfo.HeadID = Convert.ToInt32(dataReader["HeadID"].ToString());
                        objInfo.IsShow = dataReader["IsShow"].ToString();
                        objInfo.Family = dataReader["Family"].ToString();
                        objInfo.Menu_Name = dataReader["Menu_Name"].ToString();
                        objInfo.Url1 = dataReader["Url1"].ToString();
                        objInfo.Image = dataReader["Image"].ToString();
                        objInfo.Mark = dataReader["Mark"].ToString();
                    }
                }
                return objInfo;
            }
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        public TSystemMenuEntity GetEntity_SystemMenu(int id)
        {
            string strSQL = "SELECT * FROM TSystemMenu WHERE ID=@ID";
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
                TSystemMenuEntity objInfo = new TSystemMenuEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        objInfo.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        objInfo.HeadID = Convert.ToInt32(dataReader["HeadID"].ToString());
                        objInfo.IsShow = dataReader["IsShow"].ToString();
                        objInfo.Family = dataReader["Family"].ToString();
                        objInfo.Menu_Name = dataReader["Menu_Name"].ToString();
                        objInfo.Url1 = dataReader["Url1"].ToString();
                        objInfo.Image = dataReader["Image"].ToString();
                        objInfo.Mark = dataReader["Mark"].ToString();
                    }
                }
                return objInfo;
            }
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        public TSystemMenuEntity GetEntity_SystemMenuByHeadID(int headid)
        {
            string strSQL = "SELECT * FROM TSystemMenu WHERE HeadID=@HeadID ORDER BY Family DESC ";
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL))
            {
                db.AddInParameter(dbCommand, "HeadID", DbType.Int32, headid);
                TSystemMenuEntity objInfo = new TSystemMenuEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        objInfo.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        objInfo.HeadID = Convert.ToInt32(dataReader["HeadID"].ToString());
                        objInfo.IsShow = dataReader["IsShow"].ToString();
                        objInfo.Family = dataReader["Family"].ToString();
                        objInfo.Menu_Name = dataReader["Menu_Name"].ToString();
                        objInfo.Url1 = dataReader["Url1"].ToString();
                        objInfo.Image = dataReader["Image"].ToString();
                        objInfo.Mark = dataReader["Mark"].ToString();
                    }
                }
                return objInfo;
            }
        }

        /// <summary>
        /// 获取数据信息
        /// </summary>
        public DataSet GetList_SystemMeun(string isShow)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = new DataSet();
            using (DbCommand dbCommand = db.GetStoredProcCommand("ALPrc_SystemMenu"))
            {
                db.AddInParameter(dbCommand, "IsShow", DbType.String, isShow);

                ds = db.ExecuteDataSet(dbCommand);

                return (ds.Tables[0].Rows.Count < 1) ? null : ds;
            }
        }

        /// <summary>
        /// 报表中心
        /// </summary>
        public DataSet GetList_SystemMeunReport(string isShow)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = new DataSet();
            using (DbCommand dbCommand = db.GetStoredProcCommand("ALPrc_SystemMenuReport"))
            {
                db.AddInParameter(dbCommand, "IsShow", DbType.String, isShow);

                ds = db.ExecuteDataSet(dbCommand);

                return (ds.Tables[0].Rows.Count < 1) ? null : ds;
            }
        }

        /// <summary>
        /// 获取记录详细信息
        /// </summary>
        public TSystemRegistEntity GetEntity_SystemRegist(string pcCode)
        {
            string strSQL = "SELECT * FROM TSystemRegist WHERE PcCode=@PcCode";
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL))
            {
                db.AddInParameter(dbCommand, "PcCode", DbType.String, pcCode);
                TSystemRegistEntity objInfo = new TSystemRegistEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        objInfo.PcCode = dataReader["PcCode"].ToString();
                        objInfo.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        objInfo.RegCode = dataReader["RegCode"].ToString();
                        objInfo.Mark = dataReader["Mark"].ToString();
                    }
                }
                return objInfo;
            }
        }
    }
}
